﻿@inject IHttpService _httpService

@inject ILocalStorageService _localStorage


@inject IPopupService PopupService

@inject NavigationManager NavigationManager

<CnGalWebSite.Shared.MasaComponent.Shared.Cards.TitleTip Title="创建周边"/>
<AutoSaveTip TItem="CreatePeripheryViewModel" Item="Model" DataLoaded="DataLoaded" @ref="autoSave" />

<div>
    <CnGalWebSite.Shared.MasaComponent.Shared.Editors.Cards.EditSummaryTip Type="ExaminedNormalListModelType.Periphery" Class="mb-3" Creating/>
    @if (isReady == false)
    {
        <CnGalWebSite.Components.Progressings.ProgressingCard Page Fill />
    }
    else
    {
        <CnGalWebSite.Components.Containers.MasaTitleContainer Title="创建周边" Icon="@IconType.Periphery.ToIconString()" Outline>
            <CnGalWebSite.Shared.MasaComponent.Shared.Editors.Peripheries.MainCard Model="Model.Main" Class="mb-4" />
            <CnGalWebSite.Shared.MasaComponent.Shared.Editors.Peripheries.ImagesCard Model="Model.Images" Class="mb-4" />

            <CnGalWebSite.Shared.MasaComponent.Shared.Editors.Peripheries.RelatedEntriesCard Model="Model.Entries" Class="mb-4" EntryGameItems="EntryGameItems" EntryRoleItems="EntryRoleItems" EntryGroupItems="EntryGroupItems" EntryStaffItems="EntryStaffItems" />
            <CnGalWebSite.Shared.MasaComponent.Shared.Editors.Peripheries.RelatedPeripheriesCard Model="Model.Peripheries" Class="mb-4" PeripheryItems="PeripheryItems" />

            <CnGalWebSite.Shared.MasaComponent.Shared.Editors.Buttons.SubmitButton OnSubmit="OnSubmit" Model="Model" />
        </CnGalWebSite.Components.Containers.MasaTitleContainer>
    }
</div>



@code {
    private CreatePeripheryViewModel Model { get; set; } = new CreatePeripheryViewModel();

    private bool isReady = false;
    private List<string> EntryGameItems { get; set; } = new List<string>();
    private List<string> EntryRoleItems { get; set; } = new List<string>();
    private List<string> EntryGroupItems { get; set; } = new List<string>();
    private List<string> EntryStaffItems { get; set; } = new List<string>();
    private List<string> PeripheryItems { get; set; } = new List<string>();

    private bool IsOnlyNeedPublishTime { get; set; } = true;
    [CascadingParameter]
    public ErrorHandler ErrorHandler { get; set; }
    AutoSaveTip<CreatePeripheryViewModel> autoSave { get; set; }


    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            try
            {
                //获取所有词条
                EntryGameItems = await _httpService.GetAsync<List<string>>("api/entries/GetAllEntries/0");
                EntryRoleItems = await _httpService.GetAsync<List<string>>("api/entries/GetAllEntries/1");
                EntryGroupItems = await _httpService.GetAsync<List<string>>("api/entries/GetAllEntries/2");
                EntryStaffItems = await _httpService.GetAsync<List<string>>("api/entries/GetAllEntries/3");

                PeripheryItems = await _httpService.GetAsync<List<string>>("api/peripheries/GetAllPeripheries");

                isReady = true;
                StateHasChanged();
            }
            catch (Exception ex)
            {
                await ErrorHandler.ProcessError(ex, "获取所有词条名称列表失败");
            }
        }
    }

    private async Task OnSubmit()
    {
        try
        {
            var obj = await _httpService.PostAsync<CreatePeripheryViewModel, Result>("api/peripheries/CreatePeriphery", Model);
            //判断结果
            if (obj.Successful == false)
            {

                await PopupService.ToastErrorAsync("创建周边失败", obj.Error);
            }
            else
            {
                autoSave.Clean();

                await PopupService.ToastSuccessAsync("创建周边成功", "创建周边成功，等待审核通过");
                NavigationManager.NavigateTo("/peripheries/index/" + obj.Error);
            }
        }
        catch (Exception ex)
        {
            await ErrorHandler.ProcessError(ex, "创建周边失败");
        }
    }


    private void DataLoaded(CreatePeripheryViewModel model)
    {
        isReady = false;
        StateHasChanged();
        Model = model;
        isReady = true;
        StateHasChanged();
        
    }

}
